package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.api.domain.SysRole;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.dto.SysRoleDTO;
import com.linq.cool.system.dto.SysRoleDataScopeDTO;
import com.linq.cool.system.dto.SysRoleQueryDTO;
import com.linq.cool.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/25 00:36
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("role")
public class SysRoleController extends BaseController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 条件分页获取角色信息
     *
     * @param dto 角色信息
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:role:list")
    @GetMapping("list")
    public Result<?> list(SysRoleQueryDTO dto) {
        log.info("SysRoleQueryDTO:{}", dto);
        IPage<SysRole> page = sysRoleService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 根据角色id获取详细信息
     *
     * @param roleId 角色id
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:role:query")
    @GetMapping("{roleId}")
    public Result<?> getInfo(@PathVariable("roleId") Long roleId) {
        return Result.ok(sysRoleService.getById(roleId));
    }

    /**
     * 新增角色
     *
     * @param dto 角色信息
     *
     * @return 结果
     */
    @Log(moduleTitle = "角色管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "sys:role:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysRoleDTO dto) {
        return this.toResult(sysRoleService.saveRole(dto));
    }

    /**
     * 修改保存角色
     *
     * @param dto 角色信息
     *
     * @return 结果
     */
    @Log(moduleTitle = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:role:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysRoleDTO dto) {
        return this.toResult(sysRoleService.updateRole(dto));
    }

    /**
     * 修改保存数据权限
     *
     * @param dto 角色信息
     *
     * @return 结果
     */
    @Log(moduleTitle = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:role:edit")
    @PutMapping("/updateDataScope")
    public Result<?> updateDataScope(@Validated @RequestBody SysRoleDataScopeDTO dto) {
        return this.toResult(sysRoleService.updateDataScope(dto));
    }

    /**
     * 状态修改
     *
     * @return 结果
     */
    @Log(moduleTitle = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:role:edit")
    @PutMapping("/changeStatus/{roleId}/{status}")
    public Result<?> changeStatus(@PathVariable("roleId") Long roleId, @PathVariable("status") Integer status) {
        SysRole role = new SysRole();
        role.setId(roleId).setStatus(status);
        sysRoleService.checkAllowed(role);
        role.setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(sysRoleService.updateById(role));
    }


    /**
     * 根据角色id批量删除角色
     *
     * @param roleIds 角色id
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:role:remove")
    @DeleteMapping("/{roleIds}")
    public Result<?> remove(@PathVariable("roleIds") List<Long> roleIds) {
        return this.toResult(sysRoleService.deleteRoleByIds(roleIds));
    }

    /**
     * 获取角色选择框列表
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "syst:role:query")
    @GetMapping("/getOptionSelect")
    public Result<?> optionselect() {
        return Result.ok(sysRoleService.list(new LambdaQueryWrapper<SysRole>()
                                                     .eq(SysRole::getIsDeleted, UserConstants.StateEnum.STATUS_NORMAL.getCode())
                                                     .orderByAsc(SysRole::getSort)
        ));
    }
}
